<?php


namespace app\service\model;


use app\service\Y;

class AttributeModel extends BaseModel
{

    protected $table = 'attribute';

    /**
     * 保存数据
     * @param $param
     * @param $id
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function saveData($param, $id) {
        //
        if ($param['pid'] > 0) {
            $p_category = self::where('id', '=', $param['pid'])->find();
            if (!$p_category) {
                return Y::error('上级分类不存在');
            }
        }

        if ($id > 0) {
            if (self::where('name', '=', $param['name'])->where('id', '<>', $id)->find()) {
                return Y::error('分类名已存在');
            }
            $data = self::find($id);
            if (!$data) {
                return Y::error('参数错误');
            }
            $data->save($param);
        } else {
            if (self::where('name', '=', $param['name'])->find()) {
                return Y::error('分类名已存在');
            }
            self::create($param);
        }
        return Y::success([], '保存成功');
    }
}